import api from './index'

export interface LoginRequest {
  username: string
  password: string
}

export interface RegisterRequest {
  username: string
  password: string
  name: string
}

export interface AuthResponse {
  success: boolean
  message: string
  data?: {
    user: {
      id: number
      username: string
      name: string
      roles: string[]
      isAdmin?: boolean
      hasSetRoles?: boolean
      createdAt?: string
      updatedAt?: string
    }
    token: string
  }
}

export interface LoginResponse extends AuthResponse {}
export interface RegisterResponse extends AuthResponse {}

export interface RoleDefinition {
  name: string
  description: string
  permissions: string[]
}

export interface RolesResponse {
  success: boolean
  data: Record<string, RoleDefinition>
}

// 注册
export const register = async (userData: RegisterRequest): Promise<RegisterResponse> => {
  return await api.post('/register', userData)
}

// 登录
export const login = async (credentials: LoginRequest): Promise<LoginResponse> => {
  return await api.post('/login', credentials)
}

// 用户选择角色
export const selectUserRoles = async (userId: number, roles: string[]) => {
  return await api.post(`/user/${userId}/roles`, { roles })
}

// 获取角色定义
export const getRoles = async (): Promise<RolesResponse> => {
  return await api.get('/roles')
}

// 获取用户信息
export const getUserInfo = async (userId: number) => {
  return await api.get(`/user/${userId}`)
}

// 健康检查
export const healthCheck = async () => {
  return await api.get('/health')
}

// 房东认证相关接口
export interface LandlordVerificationRequest {
  userId: number
  name: string
  phone: string
  idCard: string
  address: string
  businessLicense?: string
  idCardPhoto?: string
  verificationCode: string
}

export interface LandlordVerificationResponse {
  success: boolean
  message: string
  data?: {
    verificationId: number
    status: string
    submittedAt: string
  }
}

export interface LandlordVerificationStatus {
  success: boolean
  message?: string
  data?: {
    id: number
    userId: number
    name: string
    phone: string
    idCard: string
    address: string
    businessLicense: string
    idCardPhoto: string
    status: 'pending' | 'approved' | 'rejected'
    submittedAt: string
    reviewedAt?: string
    reviewComment?: string
  }
}

// 提交房东认证
export const submitLandlordVerification = async (data: LandlordVerificationRequest): Promise<LandlordVerificationResponse> => {
  return await api.post('/landlord/verification', data)
}

// 获取房东认证状态
export const getLandlordVerificationStatus = async (userId: number): Promise<LandlordVerificationStatus> => {
  return await api.get(`/landlord/verification/${userId}`)
}
